package leju.common.widget;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 比例环
 * 
 * @author liuwei
 * 
 */

public class ColorRing extends View {
	int width = 0;
	int height = 0;
	int[] scales = null;
	int[] colors = null;
	RectF mOval = null;
	float arcWidth = 30;

	public ColorRing(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ColorRing(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public ColorRing(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {

		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(arcWidth);
		BlurMaskFilter mBGBlur = new BlurMaskFilter(4,
				BlurMaskFilter.Blur.INNER);

		mPaint.setMaskFilter(mBGBlur);

		mOval = new RectF(getPaddingLeft()+arcWidth, getPaddingTop()+arcWidth, width
				- getPaddingRight()-arcWidth, height - getPaddingBottom()-arcWidth);

		if (colors != null || scales != null || colors.length == scales.length) {
			int length = colors.length;
			int startAngle = 0;
			for (int i = 0; i < length; i++) {
				mPaint.setColor(colors[i]);
				
                
				canvas.drawArc(mOval, startAngle, scales[i] - 1, false, mPaint);
				startAngle += scales[i];
			}

		}

		super.onDraw(canvas);
	}

	public void setArcWidth(float arcWidth) {
		this.arcWidth = arcWidth;
		invalidate();
	}

	public void setColorsAndScales(int[] colors, int[] scales) {
		if (colors == null || scales == null || colors.length != scales.length) {
			throw new IllegalArgumentException(
					" colors.length must = scales.length");
		}
		this.colors = colors;
		this.scales = scales;
		invalidate();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
		height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
		setMeasuredDimension(width, height);
	}

}
